package com.xoa.controller.attend;


import com.xoa.dao.attendance.AttendLeaveParamMapper;
import com.xoa.model.attendance.AttendLeaveParam;
import com.xoa.service.attend.AttendLeaveParamService;
import com.xoa.util.ToJson;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/AttendLeaveParam")
public class AttendLeaveParamController {

    @Resource
    private AttendLeaveParamMapper attendLeaveParamMapper;

    @Autowired
    AttendLeaveParamService attendLeaveParamService;
    @ResponseBody
    @RequestMapping("/addAttendLeave")
    ToJson<AttendLeaveParam> addAttendLeave (AttendLeaveParam attendLeaveParam){
        return attendLeaveParamService.insertattendLeaveParam(attendLeaveParam);
    }


    @ResponseBody
    @RequestMapping("/updateAttendLeave")
    ToJson<AttendLeaveParam> updateAttendLeave (HttpServletRequest request, String attendLeaveParams){
        ToJson<AttendLeaveParam> json = new ToJson<AttendLeaveParam>(0, "ok");
        List<AttendLeaveParam> lists=new ArrayList();
        attendLeaveParams.trim();
        String[] Strs=attendLeaveParams.split("-");
        for (String str:Strs) {
            String[] StrO=str.split(",");
            AttendLeaveParam attendLeaveParam =new AttendLeaveParam();
            attendLeaveParam.setAlpid(Integer.parseInt(StrO[0]));
            attendLeaveParam.setWorkingYears(Integer.parseInt(StrO[1]));
            attendLeaveParam.setLeaveDay(Integer.parseInt(StrO[2]));
            lists.add(attendLeaveParam);
        }
        //清空年假表
        attendLeaveParamMapper.TruncateLeaveParam();
        for (AttendLeaveParam attendLeaveParam: lists){
            if(!attendLeaveParamService.updateattendLeaveParam(attendLeaveParam)){
                json.setMsg("error");
                json.setFlag(1);
            }
        }

        return json;
    }


    @ResponseBody
    @RequestMapping("/delAttendLeave")
    ToJson<AttendLeaveParam> delAttendLeave (Integer attendLeaveId){
        return attendLeaveParamService.deleteattendLeaveParam(attendLeaveId);
    }
    @ResponseBody
    @RequestMapping("/selectAttendLeave")
    ToJson<AttendLeaveParam> selectAttendLeave (AttendLeaveParam attendLeaveParam){
        return attendLeaveParamService.selHoliday(attendLeaveParam);
    }
}
